var db = require('../../db/db');
var publictool = require("../../publictool/publictool");

exports.port = function (req, res) {
    var requestData = '';
    req.addListener('data', function (postDataChunk) {
        requestData += postDataChunk;
    });

    req.addListener('end', function () {
        publictool.putlog(req, requestData);
        var phone = publictool.checkoutdata(res, requestData);
        if (!phone) {
            return;
        }

        var client = db.client();
        db.checkuser(client, res, true, phone.accesstoken)
            .then(function (resultvalue) {
                var product_info = phone.product_info;
                if (!product_info) {
                    publictool.returnErr(res, '无产品信息', 1);
                    client.end();
                    return;
                }
                for (var i = 0; i < product_info.length; ++i) {
                    var productvalue = product_info[i];
                    var addSql = 'INSERT INTO productInfo(bundleid,productid,price,des) VALUES(?,?,?,?)';
                    var addSqlParams = [phone.bundleId, productvalue.product_id, productvalue.price, productvalue.desc];
                    client.query(addSql, addSqlParams, function (err, result) {
                        if (err) {
                            console.log('[INSERT ERROR] - ', err.message);
                        }
                    });
                }
                publictool.returnErr(res, '操作成功', 0);
                client.end();
                return;
            });
    });
};